package com.stu.status;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stu.aq.*;
import com.stu.widget.*;

/*
 * Mode 2 Transmitter Setup
 * Left X Axis: Yaw
 * Left Y Axis: Throttle
 * Right X Axis: Roll
 * Right Y Axis: Pitch
 */
public class JoystickFragment extends Fragment {
	private JoystickView joyLeft, joyRight;
	private HorBarView mode, aux1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	  Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.joystick, container, false);
		joyLeft = (JoystickView)v.findViewById(R.id.joystickViewLeft);
		joyRight = (JoystickView)v.findViewById(R.id.joystickViewRight);
		mode = (HorBarView)v.findViewById( R.id.modeValue );
		aux1 = (HorBarView)v.findViewById( R.id.aux1Value );
		return v;
	}

	/**
	 * @param v Value from 0 to 100
	 * @param update True to update view
	 */
	public void setThrottle( int v, boolean update ) {
		joyLeft.setYPos( v );
		if( update )
			doUpdate();
	}

	/**
	 * @param v Value from 0 to 100
	 * @param update True to update view
	 */
	public void setYaw( int v, boolean update ) {
		joyLeft.setXPos( v );
		if( update )
			doUpdate();
	}

	/**
	 * @param v Value from 0 to 100
	 * @param update True to update view
	 */
	public void setRoll( int v, boolean update ) {
		joyRight.setXPos( v );
		if( update )
			doUpdate();
	}

	/**
	 * @param v Value from 0 to 100
	 * @param update True to update view
	 */
	public void setPitch( int v, boolean update ) {
		joyRight.setYPos( v );
		if( update )
			doUpdate();
	}
	
	/**
	 * @param v Value from 0 to 100
	 * @param update True to update view
	 */
	public void setMode( int v, boolean update ) {
		mode.setValue( v );
		if( update )
			doUpdate();
	}
	
	/**
	 * @param v Value from 0 to 100
	 * @param update True to update view
	 */
	public void setAux1( int v, boolean update ) {
		aux1.setValue( v );
		if( update )
			doUpdate();
	}

	private void doUpdate() {
		joyLeft.invalidate();
		joyRight.invalidate();
		mode.invalidate();
		aux1.invalidate();
	}

	public void clear() {
		joyLeft.setXPos( 50 );
		joyLeft.setYPos( 50 );
		joyRight.setXPos( 50 );
		joyRight.setYPos( 50 );
		mode.setValue( 0 );
		aux1.setValue( 0 );
		doUpdate();
	}
}
